package com.yunquan.ohana.ui.photos.adapter

import android.view.View
import android.widget.ImageView
import androidx.databinding.ViewDataBinding
import com.bumptech.glide.Glide
import com.yunquan.ohana.R
import com.yunquan.ohana.databinding.ItemPhotosBinding
import com.yunquan.ohana.ui.photos.item.PhotosItemViewModel
import com.yunquan.ohana.utils.StringUtils
import com.yunquan.ohana.utils.Utils
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter
import java.io.File

class PhotosAdapter : BindingRecyclerViewAdapter<PhotosItemViewModel>() {
    override fun onBindBinding(binding: ViewDataBinding, variableId: Int, layoutRes: Int, position: Int, item: PhotosItemViewModel?) {
        super.onBindBinding(binding, variableId, layoutRes, position, item)
        val _binding = binding as ItemPhotosBinding
        val path = item!!.path.get()
        val width = (Utils.instance.getScreenWidth() - Utils.instance.dp2px(57f)) / 3
        _binding.clPhotos.layoutParams.width = width
        _binding.clPhotos.layoutParams.height = width
        if (path == null) {
            _binding.icChecked.visibility = View.GONE
            _binding.ivPlay.visibility = View.GONE
            _binding.tvVideoTime.visibility = View.GONE
            _binding.ivPic.scaleType = ImageView.ScaleType.CENTER
            _binding.ivPic.setBackgroundResource(R.drawable.bg_r20_f6f6f6)
            _binding.ivPic.setImageResource(R.mipmap.ic_camera)
        } else {
            if (item.typeIndex.get() == 2) {
                _binding.ivPlay.visibility = View.VISIBLE
                if (!StringUtils.isEmpty(item.duration.get())) {
                    _binding.tvVideoTime.visibility = View.VISIBLE
                    _binding.tvVideoTime.text = item.duration.get()
                } else {
                    _binding.tvVideoTime.visibility = View.GONE
                }
                if (item.thumbnail.get() != null) {
                    Glide.with(_binding.root.context).load(item.thumbnail.get()?.let { File(it) }).into(_binding.ivPic)
                }
            } else {
                _binding.ivPlay.visibility = View.GONE
                _binding.tvVideoTime.visibility = View.GONE
                Glide.with(_binding.root.context).load(File(path)).into(_binding.ivPic)
            }
        }
    }
}